//
//  ViewController.m
//  09-UITableViewHomeWork_2
//
//  Created by 曹曹 on 16/7/26.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "Province.h"
#import "City.h"
#import "Town.h"
#import "AllTownsTableViewController.h"

#define kTableViewResuse @"reuseIdentifier"

@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *provinceObjectArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0;
    [self handleData];
    self.navigationItem.title = @"省份-市区";
}
#pragma mark - Data
- (void)handleData {
    NSString *sourceData = [NSString stringWithContentsOfFile:@"/Users/caocao/DLS160710/UI/HomeWork/09-UITableViewHomeWork/09-UITableViewHomeWork/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *data = [sourceData componentsSeparatedByString:@"\n"];
    self.provinceObjectArray = [NSMutableArray array];
    for (NSString *area in data) {
        if (![area hasPrefix:@" "]) {
            Province *prov = [Province townWithName:area];
            [_provinceObjectArray addObject:prov];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *prov = [_provinceObjectArray lastObject];
            City *city = [City townWithName:area];
            [prov.array addObject:city];
        } else {
            City *city = [[[_provinceObjectArray lastObject] array] lastObject];
            Town *town = [Town townWithName:area];
            [city.array addObject:town];
        }
    }
    [_provinceObjectArray removeLastObject];
}
#pragma mark - Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _provinceObjectArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Province *prov = [_provinceObjectArray objectAtIndex:section];
    return prov.array.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSMutableArray *nameArray = [NSMutableArray array];
    for (Province *prov in _provinceObjectArray) {
        [nameArray addObject:prov.name];
    }
    return [nameArray objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewResuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewResuse];
    }
    Province *prov = [_provinceObjectArray objectAtIndex:indexPath.section];
    City *city = [prov.array objectAtIndex:indexPath.row];
    NSArray *tempArray = [city.name componentsSeparatedByString:@" "];
    cell.textLabel.text = tempArray[2];
    cell.detailTextLabel.text = tempArray.lastObject;
    return cell;
}
#pragma mark - Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AllTownsTableViewController *townVC = [[AllTownsTableViewController alloc] init];
    Province *prov = [_provinceObjectArray objectAtIndex:indexPath.section];
    City *city = [prov.array objectAtIndex:indexPath.row];
    townVC.townArray = [NSArray arrayWithArray:city.array];
    NSArray *tempArray = [city.name componentsSeparatedByString:@" "];
    townVC.title = tempArray[2];
    [self.navigationController pushViewController:townVC animated:YES];
}
#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
